package cn.guetzjb.security.aspect;

import cn.guetzjb.core.constant.SecurityConstants;
import cn.guetzjb.core.exception.InnerAuthException;
import cn.guetzjb.core.utils.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class InnerAuthAspect implements Ordered {


    @Pointcut("@annotation(cn.guetzjb.security.anotation.InnerAuth)")
    public void check() {
    }

    @Around("check()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ServletUtils.getRequest();
        String fromSource = request.getHeader(SecurityConstants.FROM_SOURCE);
        if (!SecurityConstants.INNER.equals(fromSource)) {
            throw new InnerAuthException("内部接口，不允许调用");
        }
        return joinPoint.proceed();
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
